#include <QApplication>
#include "basicwindow.h"

BasicWindow::BasicWindow(QWidget *parent) : QMainWindow(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);

    init();
}

void BasicWindow::init()
{
    this->setStyleSheet("QMainWindow {border: 1px solid rgb(211, 211, 211);");
}

void BasicWindow::initTitleBar(QWidget* widget, const QString &filePath,
                               const QString &content, const ButtonStyle style, QColor color)
{
    Q_UNUSED(widget);

    titleBar = new TitleBar(filePath, content, style, color, this);

    connect(titleBar, &TitleBar::closeButtonClicked, this, &BasicWindow::onCloseButtonClicked);
    connect(titleBar, &TitleBar::minButtonClicked, this, &BasicWindow::onMinButtonClicked);
    connect(titleBar, &TitleBar::maxButtonClicked, this, &BasicWindow::onMaxButtonClicked);
    connect(titleBar, &TitleBar::restoreButtonClicked, this, &BasicWindow::onRestoreButtonClicked);
}

void BasicWindow::onCloseButtonClicked()
{
    QObject* parent = sender()->parent()->parent();
    if (parent) {
        if (parent->objectName() == "MainWindow") {
            onQuit();
            return;
        }
    }

    close();
}

void BasicWindow::onMinButtonClicked()
{
    showMinimized();
}

void BasicWindow::onMaxButtonClicked()
{
    showMaximized();
}

void BasicWindow::onRestoreButtonClicked()
{
    showNormal();
}

void BasicWindow::onShow()
{
    show();
    activateWindow();
}

void BasicWindow::onQuit()
{
    QApplication::quit();
}

void BasicWindow::onHide()
{
    hide();
}
